#include "util.h"
#include "ai.h"
#include "gui.h"
#include <math.h>
#include <string.h>

extern Board* plansza;
extern Gui gui;

/* pobierz pozycję na podstawie klikniętych współrzędnych
 * i rozmiaru okna */
Vector translate_point_to_coord(int boardSize, Vector clicked, Vector windowSize) {

	int tileX;
	int tileY;
	Vector ret;
	
	tileX = windowSize.x / boardSize;
	tileY = windowSize.y / boardSize;

	ret.x = clicked.x / tileX;
	ret.y = clicked.y / tileY;

	if(ret.x < 0 || ret.x > boardSize) {
		ret.x = -1;
		ret.y = -1;
	}
	else if(ret.y < 0 || ret.y >= boardSize) {
		ret.x = -1;
		ret.y = -1;
	}

	return ret;
	
}

#define BUFFER_SIZE 50

/* odśwież okienko */
void refresh_display() {

	char buffer[BUFFER_SIZE];

	Result result;

	if(plansza != NULL) {

		gtk_widget_show_all(GTK_WIDGET(gui.fixed));

		result = board_result(plansza);

		/* WYNIK */
	
		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%.1f", result.circleResult);
		gtk_label_set_label (gui.circleResult, buffer);

		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%.1f", result.crossResult);
		gtk_label_set_label (gui.crossResult, buffer);

		/* LICZBA PIONKÓW */

		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%u", result.circleSymbols);
		gtk_label_set_label (gui.circleSymbols, buffer);

		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%u", result.crossSymbols);
		gtk_label_set_label (gui.crossSymbols, buffer);

		/* LICZBA GRUP */

		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%u", result.circleGroups);
		gtk_label_set_label(gui.circleGroups, buffer);

		memset(buffer, 0, BUFFER_SIZE);
		sprintf(buffer, "%u", result.crossGroups);
		gtk_label_set_label(gui.crossGroups, buffer);

	}

	/* refresh okienek */
	gtk_widget_queue_draw(GTK_WIDGET(gui.plansza));
	gtk_widget_queue_draw(GTK_WIDGET(gui.currentMove));
	
}

/* odrysuj zadaną planszę */
gboolean draw_board_on_drawing_area(GtkWidget* drawingArea, Board* b)
{

	int width, height;
	int tileSizeX, tileSizeY;
	int rozmiarPlanszy;
	int i, j;
	symbol s;
	GtkAllocation alloc;
	cairo_t* cr;
	double SYMBOL_WIDTH;

	GdkColor circleColor;
	GdkColor crossColor;

	GdkColor circleGroupColor;
	GdkColor crossGroupColor;

	double PI;

	PI = atan(1.0)*4.0;

	if(b == NULL)
		return FALSE;

	cr = gdk_cairo_create(drawingArea->window);

	/* czerwone kółko */
	gdk_color_parse("#FF0000", &circleColor);

	/* niebieski krzyżyk */
	gdk_color_parse("#0000FF", &crossColor);

	/* kolory dla grup */
	gdk_color_parse("#FFCCCC", &circleGroupColor);
	gdk_color_parse("#CCCCFF", &crossGroupColor);

	gtk_widget_get_allocation(drawingArea, &alloc);

	width = alloc.width;
	height = alloc.height;

	rozmiarPlanszy = board_size(b);

	tileSizeX = (int)((double)width/rozmiarPlanszy);
	tileSizeY = (int)((double)height/rozmiarPlanszy);

	/* wyliczamy grubość jako 1/10 rozmiaru płytki */
	SYMBOL_WIDTH = 0.1*tileSizeX;
	
	for(i=0;i<rozmiarPlanszy;i++) {

		for(j=0;j<rozmiarPlanszy;j++) {

			s = board_get (b, i, j);

			if(s == CROSS) {
				/* wypełnij prostokąt na jasny kolorek */
				cairo_rectangle (cr, i*tileSizeX, j*tileSizeY, tileSizeX, tileSizeY);
				gdk_cairo_set_source_color (cr, &crossGroupColor);
				cairo_fill(cr);
			}
			else if(s == CIRCLE) {
				/* wypełnij prostokąt na jasny kolorek */
				cairo_rectangle (cr, i*tileSizeX, j*tileSizeY, tileSizeX, tileSizeY);
				gdk_cairo_set_source_color (cr, &circleGroupColor);
				cairo_fill(cr);
			}

			cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);

			cairo_rectangle (cr, i*tileSizeX, j*tileSizeY, tileSizeX, tileSizeY);

			cairo_set_line_width (cr, 1.0);
			cairo_stroke(cr);

			if(s == CROSS) {

				cairo_set_line_width (cr, SYMBOL_WIDTH);
				gdk_cairo_set_source_color(cr, &crossColor);

				cairo_new_path(cr);

				cairo_move_to (cr, (i+0.25)*tileSizeX, (j+0.25)*tileSizeY);
				cairo_line_to (cr, (i+0.75)*tileSizeX, (j+0.75)*tileSizeY);

				cairo_stroke(cr);

				cairo_new_path(cr);

				cairo_move_to(cr, (i+0.75)*tileSizeX, (j+0.25)*tileSizeY);
				cairo_line_to(cr, (i+0.25)*tileSizeX, (j+0.75)*tileSizeY);
				
				cairo_stroke(cr);

			}
			else if(s == CIRCLE) {

				gdk_cairo_set_source_color(cr, &circleColor);

				cairo_arc (cr, (i+0.5)*tileSizeX, (j+0.5)*tileSizeY,
				           tileSizeX/4.0, 0.0, 2.0*PI);

				cairo_set_line_width (cr, SYMBOL_WIDTH);
				cairo_stroke(cr);

			}
			
		}
		
	}

	return FALSE;
	
}

/* wyświetlenie zwycięzcy */
void show_winner() {

	Result res;
	GtkWidget* msg;

	res = board_result (plansza);

	/* nie ma remisów */
	if(res.circleResult > res.crossResult) {
		/* wygrywa kółko */
		msg = gtk_message_dialog_new(gui.main, 0, GTK_MESSAGE_INFO,
		                             GTK_BUTTONS_OK, "Kółko wygrywa!");
		gtk_dialog_run(GTK_DIALOG(msg));
	}
	else {
		/* wygrywa krzyżyk */
		msg = gtk_message_dialog_new(gui.main, 0, GTK_MESSAGE_INFO,
		                             GTK_BUTTONS_OK, "Krzyżyk wygrywa!");
		gtk_dialog_run(GTK_DIALOG(msg));
	}

	gtk_widget_destroy(msg);
	
}

/* wyświetl okienko dialogowe wyboru pliku i wczytaj planszę */
int read_from_file(Board** b, GtkWidget* parentWindow) {

	GtkWidget* dialog;
	GtkFileFilter* filter;
	int result;

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "Omringa file format (.off)");
	gtk_file_filter_add_pattern(filter, "*.off");

	dialog = (GtkWidget*)gtk_file_chooser_dialog_new("Otwórz plik",
	                  GTK_WINDOW(parentWindow),
	                  GTK_FILE_CHOOSER_ACTION_OPEN,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);

	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
	gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	  {
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		result = board_import(b, filename, FALSE, FALSE);
		g_free (filename);
		gtk_widget_destroy (dialog);
	  }
	/* zamykamy okienko i opuszczamy procedurę */
	else {
		gtk_widget_destroy (dialog);
		return -1;
	}

	if(result == -1) {

		GtkMessageDialog* dlg;

		dlg = (GtkMessageDialog*)gtk_message_dialog_new(GTK_WINDOW(parentWindow),
		                                GTK_DIALOG_MODAL, GTK_MESSAGE_INFO,
		                             	GTK_BUTTONS_OK, "Nieprawidłowy plik!");

		gtk_dialog_run(GTK_DIALOG(dlg));
		gtk_widget_destroy (GTK_WIDGET(dlg));
		
	}

	return result;

}

/* wyświetl okienko dialogowe zapisu do pliku i zapisz */
int save_to_file(Board* b, GtkWidget* parentWindow) {

	GtkWidget* dialog;
	GtkFileFilter* filter;
	int result = 0;

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "Omringa file format (.off)");
	gtk_file_filter_add_pattern(filter, "*.off");

	dialog = gtk_file_chooser_dialog_new("Zapisz stan gry", GTK_WINDOW(parentWindow),
	                  GTK_FILE_CHOOSER_ACTION_SAVE,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
				      NULL);

	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
	gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	  {
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		result = board_export(b, filename);
		g_free (filename);
	  }
	
	gtk_widget_destroy (dialog);

	return result;

}